package org.sadhar.sipp.perekamanitembiaya;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.sadhar.cektabel.cekTabel;
import org.sadhar.cektabel.cekTabelDAO;
import org.sadhar.cektabel.cekTabelDAOImpl;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassUtility;

public class PerekamanItemBiayaDAOImpl implements PerekamanItemBiayaDAO {

    public PerekamanItemBiayaDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    @Override
    public void delete(PerekamanItemBiaya item) throws Exception {
        String sql = "DELETE FROM ppmk.itemBiaya WHERE kodeItem=?";
        ClassConnection.getJdbc().update(sql, new Object[]{Integer.valueOf(item.getKodeItem())});
    }

    @Override
    public void insert(PerekamanItemBiaya item) throws Exception {
        String sql = "INSERT INTO ppmk.itemBiaya(namaItem,kodePosBiaya,kodeSatuan) " + "VALUES(?,?,?)";
        ClassConnection.getJdbc().update(
                sql,
                new Object[]{ClassUtility.quote(item.getNamaItem()),
                    Integer.valueOf(item.getKodePosBiaya()),
                    Integer.valueOf(item.getKodeSatuan())
                });
    }

    @Override
    public List<PerekamanItemBiaya> list() {
        List<PerekamanItemBiaya> items = new ArrayList<PerekamanItemBiaya>();
        List<Map> rowData = ClassConnection.getJdbc().queryForList(
                "SELECT * FROM ppmk.itemBiaya");
        for (Map m : rowData) {
            PerekamanItemBiaya itemBiaya = new PerekamanItemBiaya();
            itemBiaya.setKodeItem(Integer.valueOf(m.get("kodeItem").toString()));
            itemBiaya.setNamaItem(m.get("namaItem").toString());
            itemBiaya.setKodePosBiaya(Integer.valueOf(m.get("kodePosBiaya").toString()));
            itemBiaya.setKodeSatuan(Integer.valueOf(m.get("kodeSatuan").toString()));
            items.add(itemBiaya);
        }
        return items;
    }

    @Override
    public void update(PerekamanItemBiaya item) throws Exception {
        String sql = "UPDATE ppmk.itemBiaya SET namaItem=?,kodePosBiaya=?,kodeSatuan=? WHERE kodeItem=?";
        ClassConnection.getJdbc().update(
                sql,
                new Object[]{ClassUtility.quote(item.getNamaItem()),
                    Integer.valueOf(item.getKodePosBiaya()),
                    Integer.valueOf(item.getKodeSatuan()),
                    Integer.valueOf(item.getKodeItem())
                });

    }

    public List<Map> select(String namaItem, int kodePosBiaya) {
        String sql = "SELECT * FROM ppmk.itemBiaya WHERE namaItem=? AND kodePosBiaya=?";
        return ClassConnection.getJdbc().queryForList(sql, new Object[]{ClassUtility.quote(namaItem),
                    Integer.valueOf(kodePosBiaya)});
    }

    public List<Map> select(String namaItem, int kodePosBiaya, int kodeItem) {
        String sql = "SELECT * FROM ppmk.itemBiaya WHERE namaItem=? AND kodePosBiaya=? AND kodeItem<>?";
        return ClassConnection.getJdbc().queryForList(sql, new Object[]{ClassUtility.quote(namaItem),
                    Integer.valueOf(kodePosBiaya), Integer.valueOf(kodeItem)});
    }

    public List<Map> select(int kodePosBiaya) {
        String sql = "select * from ppmk.itemBiaya WHERE kodePosBiaya=?";
        List<Map> rowData = ClassConnection.getJdbc().queryForList(sql, new Object[]{Integer.valueOf(kodePosBiaya)});
        return rowData;
    }

     @Override
    public List<PerekamanItemBiaya> listItemBiayaPerPos(int kodePosBiaya) {
        String sql = "SELECT * FROM ppmk.itemBiaya WHERE kodePosBiaya=?";

        List<PerekamanItemBiaya> items = new ArrayList<PerekamanItemBiaya>();

        List<Map> rowData = ClassConnection.getJdbc().queryForList(sql, new Object[]{Integer.valueOf(kodePosBiaya)});
        for (Map m : rowData) {
            PerekamanItemBiaya itemBiaya = new PerekamanItemBiaya();
            itemBiaya.setKodeItem(Integer.valueOf(m.get("kodeItem").toString()));
            itemBiaya.setNamaItem(m.get("namaItem").toString());
            itemBiaya.setKodePosBiaya(Integer.valueOf(m.get("kodePosBiaya").toString()));
            itemBiaya.setKodeSatuan(Integer.valueOf(m.get("kodeSatuan").toString()));
            items.add(itemBiaya);
        }
        return items;
    }
     // revisied by : sarah (BAPSI) / tgl:10-12-2009
    public boolean cekDataTabel(String table, String[] field, String[] value) {
        cekTabel obj = new cekTabel();
        cekTabelDAO dao = new cekTabelDAOImpl();
        List lsNamaField = new ArrayList();
        List lsValueField = new ArrayList();
        for (int i = 0; i < field.length; i++) {
            lsNamaField.add(field[i].toString());
            lsValueField.add(value[i].toString());
        }
        obj.setNamaTabel(table);
        obj.setNamaField(lsNamaField);
        obj.setValueField(lsValueField);
        if (dao.cekTabel(obj)) {
            return true;
        } else {
            return false;
        }
    }
}
